package com.courseselector.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.courseselector.common.Result;
import com.courseselector.entity.user.User;
import com.courseselector.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 教师管理控制器
 */
@Api(tags = "教师管理")
@RestController
@RequestMapping("/teacher")
public class TeacherController {
    
    @Autowired
    private UserService userService;
    
    @ApiOperation("分页查询教师列表")
    @GetMapping("/list")
    public Result<Page<User>> list(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer page,
            @ApiParam("每页记录数") @RequestParam(defaultValue = "10") Integer size) {
        Page<User> pageParam = new Page<>(page, size);
        Page<User> pageResult = userService.getTeacherList(pageParam);
        return Result.success(pageResult);
    }
    
    @ApiOperation("根据ID获取教师信息")
    @GetMapping("/{id}")
    public Result<User> getById(@ApiParam("教师ID") @PathVariable Integer id) {
        User teacher = userService.getTeacherById(id);
        if (teacher == null) {
            return Result.fail("教师不存在");
        }
        return Result.success(teacher);
    }
    
    @ApiOperation("添加教师")
    @PostMapping
    public Result<Boolean> add(@ApiParam("教师信息") @RequestBody User user) {
        boolean result = userService.addTeacher(user);
        return result ? Result.success() : Result.fail("添加教师失败");
    }
    
    @ApiOperation("更新教师信息")
    @PutMapping
    public Result<Boolean> update(@ApiParam("教师信息") @RequestBody User user) {
        boolean result = userService.updateTeacher(user);
        return result ? Result.success() : Result.fail("更新教师信息失败");
    }
    
    @ApiOperation("删除教师")
    @DeleteMapping("/{id}")
    public Result<Boolean> delete(@ApiParam("教师ID") @PathVariable Integer id) {
        boolean result = userService.deleteTeacher(id);
        return result ? Result.success() : Result.fail("删除教师失败");
    }
} 